package edu.unice.simul.vue;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import edu.unice.simul.controleur.ConfigControleur;
import edu.unice.simul.listeners.ConfigListener;
import edu.unice.simul.modele.contenus.mobiles.Humain;
import edu.unice.simul.vue.components.config.ModeleJtable;

/**
 * Fenêtre de configuration (paramétrage des humains)
 * 
 * @author Romain Ciaccafava
 * 
 */
public class ConfigFrame extends JFrame implements ConfigListener {
	private static final long serialVersionUID = -4719409332443216097L;

	private final static JTable tableau = new JTable();
	private final JButton editer = new JButton("Editer");
	private final JButton generer = new JButton("Generer aléatoirement");
	private final JButton ok = new JButton("Commencer");

	private final ConfigControleur controleur;

	public ConfigFrame(ConfigControleur configControleur) {
		this.controleur = configControleur;
		buildUI();
		addListeners();
		pack();

		setVisible(true);

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

		setLocation((screenSize.width - getWidth()) / 2,
				(screenSize.height - getHeight()) / 2);
	}

	/**
	 * Ajoute les écouteurs
	 */
	private void addListeners() {
		editer.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int selection = tableau.getSelectedRow();

				Humain humain = controleur.getHumain(selection);

				EditFrame fenedit = new EditFrame(humain);
				fenedit.setVisible(true);
			}
		});

		ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				controleur.start();
			}
		});

		generer.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				controleur.generate();
			}
		});

		tableau.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {
					@Override
					public void valueChanged(ListSelectionEvent e) {
						editer.setEnabled(e.getFirstIndex() >= 0);
					}
				});
	}

	/**
	 * Construit l'interface
	 */
	private void buildUI() {
		editer.setEnabled(false);
		setLayout(new BorderLayout());
		this.setTitle("Configuration");
		tableau.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		JLabel titre = new JLabel("Configuration", JLabel.CENTER);
		titre.setFont(new Font("Arial", Font.BOLD, 22));
		JPanel configPanel = new JPanel();
		configPanel.setLayout(new BorderLayout());
		configPanel.setBorder(BorderFactory
				.createTitledBorder("Gestion des humains"));

		configPanel.add(new JScrollPane(tableau), BorderLayout.CENTER);
		JPanel buttonsWraper = new JPanel(new BorderLayout());
		JPanel buttonsPanel = new JPanel(new GridLayout(2, 1));
		buttonsPanel.add(editer);
		buttonsPanel.add(generer);
		buttonsWraper.add(buttonsPanel, BorderLayout.NORTH);

		configPanel.add(buttonsWraper, BorderLayout.EAST);

		add(titre, BorderLayout.NORTH);
		add(configPanel, BorderLayout.CENTER);

		JPanel okPanel = new JPanel(new BorderLayout());
		okPanel.add(ok, BorderLayout.EAST);
		add(okPanel, BorderLayout.SOUTH);
	}

	@Override
	public void refreshHumains(List<Humain> humains) {
		tableau.setModel(new ModeleJtable(humains));

	}

	/**
	 * Met à jour la jtable après l'édition d'un humain
	 */
	public static void refreshHumain() {
		tableau.updateUI();
	}

}